﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

using Survey.DataAccessLayer.Data;
using Survey.Entity;

namespace Survey.DataAccessLayer
{
    public class UserAccessor : Accessor<User>
    {
        public UserAccessor() : base(new SurveyDbContext()) { }
        public User GetUser(int id)
        {
            var q = Select(user => user.Id == id);
            return q.Single();
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <param name="questionnaireCollection"></param>
        /// <returns></returns>
        public User Add(string email, string password, ICollection<Questionnaire> questionnaireCollection)
        {
            string passwordHash = String2MD5Hash(password);
            User user = new User
            {
                Email = email,
                PasswordHash = passwordHash,
                QuestionnaireCollection = questionnaireCollection != null ? questionnaireCollection : new List<Questionnaire>()
            };
            Add(user);
            return user;
        }
        /// <summary>
        /// 判断邮箱和密码是否匹配
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public User UserValidation(string email, string password)
        {
            string passwordHash = String2MD5Hash(password);
            return this.Select(
                user => user.Email.Equals(email) && user.PasswordHash.Equals(passwordHash)).Single();
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="user"></param>
        /// <param name="oldPassword"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        public bool ModifyPassword(User user, string oldPassword, string newPassword)
        {
            string aheadPasswordHash = String2MD5Hash(oldPassword);
            if (user.PasswordHash.Equals(aheadPasswordHash))
            {
                string backPasswordHash = String2MD5Hash(newPassword);
                user.PasswordHash = backPasswordHash;
                Update(user);
                Save();
            }
            return false;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="user"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        public bool ModifyPassword(User user, string newPassword)
        {
            if (newPassword.Trim().Length > 0)
            {
                string newPasswordHash = String2MD5Hash(newPassword);
                user.PasswordHash = newPasswordHash;
            }
            Update(user);
            return Save() > 0;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="email"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        public bool ModifyPassword(string email, string newPassword)
        {
            User user = Select(u => u.Email.Equals(email)).Single();
            return ModifyPassword(user, newPassword);
        }
        #region Helper
        private string String2MD5Hash(string str)
        {
            MD5 md5 = MD5.Create();
            byte[] buffer = Encoding.UTF8.GetBytes(str);
            return Convert.ToBase64String(md5.ComputeHash(buffer));
        }
        #endregion
    }
}
